package com.jacen.fzxs.service.app.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import jodd.util.StringUtil;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jacen.fzxs.entity.UserEntity;
import com.jacen.fzxs.entity.web.AppEntity;
import com.jacen.fzxs.exception.ParamException;
import com.jacen.fzxs.exception.PermissionException;
import com.jacen.fzxs.exception.WebException;
import com.jacen.fzxs.service.IUserService;

@Service
@Transactional(rollbackFor = Exception.class)
public class LoginHandlerService extends BaseHandlerService {
	@Resource
	private IUserService userService;

	@Override
	protected AppEntity submit(AppEntity appEntity,HttpServletRequest request) throws WebException {
		AppEntity resAppEntity = new AppEntity();
		String userName = appEntity.getUserName();
		String password = appEntity.getPassword();
		if(StringUtil.isEmpty(userName)){
			throw new ParamException("用户名不能为空");
		}
		if(StringUtil.isEmpty(password)){
			throw new ParamException("密码不能为空");
		}
		UserEntity user = userService.login(userName, password);
		if(user==null){
			throw new PermissionException("用户名和密码不正确");
		}
		resAppEntity.setUserName(userName);
		resAppEntity.setPassword(password);
		resAppEntity.setShowName(user.getShowName());
		return resAppEntity;
	}

}
